package com.gdc.cloud.controller;


import com.gdc.cloud.entities.CommonResult;
import com.gdc.cloud.entities.Payment;
import com.gdc.cloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * Description: 支付 Controller
 * @author: gdc
 * @date: 2021/1/31
 * @version 1.0
 */
@RestController
@Slf4j
public class PaymentController {

    /**
     * 服务端口号
     */
    @Value("${server.port}")
    private String serverPort;

    @Resource
    private PaymentService paymentService;
    @Resource
    private DiscoveryClient discoveryClient;

    /**
     * 创建 支付信息
     * @param payment               支付信息
     * @return                      结果
     */
    @PostMapping(value = "/payment/create")
    public CommonResult<Integer> create(Payment payment) {
        int result = paymentService.create(payment);
        log.info("插入结果:"+result);
        if (result > 0) {
            return new CommonResult<>(200, "插入成功,serverPort:" + serverPort, result);
        }else {
            return new CommonResult<>(444, "插入失败,serverPort:" + serverPort, null);
        }
    }

    /**
     * 通过ID获取支付信息
     * @param id                    ID
     * @return                      支付信息
     */
    @GetMapping(value = "/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        Payment paymentById = paymentService.getPaymentById(id);
        log.info("插入结果:"+ paymentById);
        if (paymentById!=null) {
            return new CommonResult<>(200, "查询成功,serverPort:" + serverPort, paymentById);
        }else {
            return new CommonResult<>(444, "没有对应记录,serverPort:" + serverPort + "，查询id" + id, null);
        }
    }

    /**
     * 服务发现接口
     * @return                      服务发现信息
     */
    @GetMapping(value = "/payment/discovery")
    public Object discovery() {
        discoveryClient.getServices().forEach(service -> log.info("*****element:" + service));
        discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE").forEach(instance ->
                log.info(instance.getServiceId()+"\t" + instance.getHost() + "\t" + instance.getPort() +"\t" + instance.getUri()));
        return  this.discoveryClient;
    }

    /**
     * 通过自定义 负载均衡类 来测试获取获取数据
     * @return                      结果
     */
    @GetMapping("/payment/lb")
    public String lb(){
        return "端口号：" + serverPort;
    }

    /**
     * 测试 OpenFeign 的超时控制功能（对请求的返回结果延时 3s 返回）
     * @return                      结果
     */
    @GetMapping(value = "/payment/feign/timeout")
    public String paymentFeignTimeout() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            return serverPort;
        }
    }

    /**
     * 测试链路追踪服务接口
     * @return                      结果
     */
    @GetMapping(value="/payment/zipkin")
    public String paymentZipkin() {
        return "hello,i am paymentZipkin server fallback,O(∩_∩)O哈哈~";
    }
}